package spil;

import desktop_resources.GUI;

public class Fleet extends Ownable {
	protected final static int RENT_1 = 500;
	protected final static int RENT_2 = 1000;
	protected final static int RENT_3 = 2000;
	protected final static int RENT_4 = 4000;
	
	private Player owner;
	
	public Fleet(int price, int fieldNumber, String title, String subText, String description) {
		super(fieldNumber, title, subText, description);
		this.price = price;
	}
	
	public Fleet() {
		super();
	}
	
	public void landOnField(Player p) {
		if(owner == null) {
			boolean buy = GUI.getUserLeftButtonPressed(title + " is not owned", "buy", "ignore");
			if(buy) {
				buy(p);
			}
			else GUI.getUserButtonPressed(p + " did not buy " + title, "ok");
		}
		else {
			if(p.account.getBalance() < getRent()) {
				GUI.getUserButtonPressed(p + " pays " + p.getBalance() + "\n to " + owner, "ok");
				owner.account.insert(p.getBalance());
				p.account.withdraw(p.getBalance());
			}
			else if(p!=owner) {
				GUI.getUserButtonPressed(p + " pays " + getRent() + "\n to " + owner, "ok");
				p.account.withdraw(getRent());
				owner.account.insert(getRent());
			}
			else GUI.getUserButtonPressed(owner + " owns " + title, "ok");
		}
	}
	
	public void lose() {
		owner = null;
	}
	
	public int getRent() {
		switch(owner.getFleets()) {
		case 1: return RENT_1;
		case 2: return RENT_2;
		case 3: return RENT_3;
		case 4: return RENT_4;
		default: return 0;
		}
	}
	
	public void buy(Player buyer) {
		if(buyer.getBalance() > price) {
			owner = buyer;
			buyer.account.withdraw(price);
			buyer.addFleet();
			GUI.getUserButtonPressed(buyer + " bought " + title, "ok");
		}
		else GUI.getUserButtonPressed(buyer + " cannot afford " + title, "ok");
	}
	
	public String toString() {
		if(owner != null)
			return title +  " owned by " + owner;
		return title + " isn't owned" ;
	}
}
